﻿using Common.Commands;
using Common.Commands.MemberCommands;
using Core.Domain.Model;
using Core.Infrastructure.CustomExceptions;
using Sanelib.Common.Helpers;
using Sanelib.DataOperation;
using Sanelib.Dto;

namespace Core.Processors.MemberProcessors
{
    public class DeleteMemberProcessor : ICommandProcessor<DeleteMember>
    {
        private readonly IRepository<Member> _memberRepository;

        public DeleteMemberProcessor(IRepository<Member> memberRepository)
        {
            _memberRepository = memberRepository;
        }

        public dynamic Process(DeleteMember command)
        {
            var admin = _memberRepository.GetByKey(Property.Of<Member>(x => x.Code), "admin");
            if (admin != null && admin.Id == command.Id)
            {
                throw new DomainProcessException("Admin user can not be deleted");
            }

            var guest = _memberRepository.GetByKey(Property.Of<Member>(x => x.Code), "guest");
            if (guest != null && guest.Id == command.Id)
            {
                throw new DomainProcessException("Guest user can not be deleted");
            }

            _memberRepository.Delete(command.Id);
            var response = new WebApiResultResponse { IsSuccess = true };
            return response;
        }
    }
}